/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package multiThreadedressbook;

import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author potirond
 */
public class Contact implements Runnable{
    protected String name;
    protected String firstname;
    protected String homeAddress;
    protected String companyAddress;
    protected String persMobileNum;
    protected String homeNum;
    protected String officeNum;
    protected String profMobileNum;
    protected String email;
    protected String photo;
    protected String other;

    public Contact() {

    }

    public static void newContact(String name,String firstname,String homeAddress,String compagnyAddress,String persMobileNum,String homeNum,String officeNum,String profMobileNum,String email,String photo,String groups,String website) throws FileNotFoundException, IOException{
          File csvFile = new File("contacts.csv"); 
			if (!csvFile.exists()) 
				throw new FileNotFoundException("Le fichier n'existe pas"); 
			else{
			PrintStream l_out = new PrintStream(new FileOutputStream("contacts.csv",true)); 
 
 
 
			l_out.print(name +";"); 
			l_out.print(firstname +";");
                        l_out.print(homeAddress + ";");
                        l_out.print(compagnyAddress + ";");
                        l_out.print(persMobileNum + ";");
                        l_out.print(homeNum + ";");
                        l_out.print(officeNum + ";");
                        l_out.print(profMobileNum + ";");
                        l_out.print(email + ";");
                        l_out.print(photo + ";");
                        l_out.print(groups + ";");
                        l_out.print(website + "\n");
 
			l_out.flush(); 
			l_out.close(); 
			l_out=null;
                        }
    }
    
    public static ArrayList<ArrayList> read() throws FileNotFoundException, IOException{
        String FILE_NAME = "contacts.csv";

        File file = new File(FILE_NAME);
        StringTokenizer lineParser; 
        BufferedReader reader = new BufferedReader(new FileReader(file));
        ArrayList<ArrayList> dataTable = new ArrayList<ArrayList>(); 
        ArrayList<String> dataRow; 
        String line = null; 
        String value = null;
        
        while ((line = reader.readLine()) != null) { 
            dataRow = new ArrayList<String>(); 
            lineParser = new StringTokenizer(line, ";"); 
            while (lineParser.hasMoreElements()) { 
                value = (String) lineParser.nextElement(); 
                dataRow.add(value); 
            } 
        dataTable.add(dataRow); 
        }
        return dataTable;
    }
    
    /**
     *
     * @param contacts
     * @throws FileNotFoundException
     */
    public static void delete(ArrayList<ArrayList> contacts) throws FileNotFoundException{
            File csvFile = new File("contacts.csv"); 
                            if (!csvFile.exists()) 
                                    throw new FileNotFoundException("Le fichier n'existe pas"); 
                            else{
                            PrintStream l_out = new PrintStream(new FileOutputStream("contacts.csv"));
                            
                            for(int i=0;i<contacts.size();i++){
                                for(int j=0;j<12;j++){
                                    l_out.print(contacts.get(i).get(j) + ";");
                                }
                                l_out.print("\n");
                            }
                            
                            l_out.flush(); 
                            l_out.close(); 
                            l_out=null;
                            }
    }

    public void run() {
        ArrayList<ArrayList> contacts;
        contacts=Controller.getContacts();
        try {
            Contact.delete(contacts);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Contact.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
